package com.woniuxy.exam.controller;

import com.woniuxy.exam.common.Result;
import com.woniuxy.exam.dto.AddOrderDto;
import com.woniuxy.exam.dto.OrdersDto;
import com.woniuxy.exam.entity.Movies;
import com.woniuxy.exam.entity.Orders;
import com.woniuxy.exam.service.IMoviesService;
import com.woniuxy.exam.service.IOrdersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.jaxb.SpringDataJaxb;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lzj
 * @since 2023-10-31
 *
 */
@Api(tags = "订单管理")
@RestController
@RequestMapping("/orders")
public class OrdersController {
    @Autowired
    private IOrdersService  orderService;
    @Autowired
    private IMoviesService movieService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 产生订单
     */
    @PostMapping("/addOrder")
    @CacheEvict(cacheNames = "order:list",allEntries = true)
    public Result createOrder(@RequestParam("userId") int userId, @RequestParam("movieId") int movieId, @RequestParam("quantity") int quantity) {
        Map<String, Object> result = new HashMap<>();
        double totalPrice = orderService.calculateTotalPrice(userId, movieId, quantity);
        result.put("totalPrice", totalPrice);
        Orders orders = new Orders();
        orders.setMovieId(movieId);
        orders.setQuantity(quantity);
        orders.setUserId(userId);
        orders.setTotalPrice(BigDecimal.valueOf(totalPrice));
        orders.setOrderDate(new Date());
        orderService.saveOrUpdate(orders);
        return Result.ok(result);
    }

    @GetMapping("/queryAll")
    @Cacheable(cacheNames = "order:list")
    public Result findAll(){
        List<OrdersDto> ordersDtoList = orderService.findAll();
        return Result.ok(ordersDtoList);
    }


    @PostMapping("/create")
    @ApiOperation("下订单")
    //用于清除缓存删除或修改这注解才有效
   // @CacheEvict(cacheNames = "order:list",allEntries = true)
    public BigDecimal addOrder(@RequestBody AddOrderDto addOrderDto){
        System.out.println("addOrderDto"+addOrderDto);
        Orders orders = new Orders();
        orders.setUserId(addOrderDto.getUserId());
        orders.setMovieId(addOrderDto.getMovieId());
        orders.setQuantity(addOrderDto.getQuantity());
        orders.setImgUrl(addOrderDto.getImgUrl());
        Movies movies = movieService.getById(addOrderDto.getMovieId());
        BigDecimal totalPrice = movies.getPrice().multiply(new BigDecimal(addOrderDto.getQuantity()));
        orders.setTotalPrice(totalPrice);
        orders.setOrderDate(new Date());
        orderService.save(orders);
        return totalPrice;
    }


    //查询所有订单
    @ApiOperation("查询所有订单")
    @GetMapping("/")
    public List<OrdersDto> queryAll() {
        List orderList = redisTemplate.opsForList().range("orderList",0,-1);
        return orderList;
    }

    @PostMapping("/")
    @ApiOperation("产生订单")
    public String addOrder(@RequestBody Orders orders) {
        redisTemplate.delete("orderList");
        orderService.save(orders);
        List<OrdersDto> ordersDtos = orderService.queryALL();
        redisTemplate.opsForList().leftPushAll("orderList",ordersDtos);
        return orders.getTotalPrice().toString();
    }

}
